package com.yanqu.road.server.gameplayer.module.activity.cat.action;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.activity.cat.*;
import com.yanqu.road.server.gameplayer.module.activity.cat.CatActivityConfig;
import com.yanqu.road.server.gameplayer.module.activity.cat.CatConnectExcetpion;
import com.yanqu.road.server.gameplayer.module.activity.cat.ConnectResult;
import com.yanqu.road.server.manger.activity.cat.CatMgr;

public class NotNullElementAction extends BaseElementAction {

    public NotNullElementAction(int type) {
        super(type);
    }

    @Override
    boolean accept(Element element, Element next, ConnectResult context) {
        return false;
    }

    @Override
    void connect(Element element, ConnectResult context) {
        throw new CatConnectExcetpion(-1);
    }

    @Override
    void change(Element element, ConnectResult context) {
        DharmaElementAction.onChange(element, context);
        super.changeNext(context);
    }

    @Override
    void attack(Element element, ConnectResult context) {
        super.attackNext(context);
    }

    @Override
    void generate(Element element, ConnectResult context) {
        if(element.getType() != eElementTypes.NotNull.getType()){
            throw new CatConnectExcetpion(-1);
        }
        //电车、香料盒子,自己生成，如果掉落注意不要清除属性
        if(CatMgr.hasAttr(element, eElementAttrTypes.SpiceBox.getType()) != null
                || CatMgr.hasAttr(element, eElementAttrTypes.Tram.getType()) != null){
            //调用下一个元素，重要！！！
            super.generateNext(context);
            return;
        }

        //空的，向上找，看是否有达摩
        for (int i = element.getY() + 1; i < CatActivityConfig.ROW_NUM; i++) {
            Element up = CatMgr.getElement(context.getElements(), element.getX(), i);
            if(up.getType() == eElementTypes.Dharma.getType()
                    && CatMgr.hasAttr(up, eElementAttrTypes.SpiceBox.getType()) == null
                    && CatMgr.hasAttr(up, eElementAttrTypes.Tram.getType()) == null){
                //CatMgr.getLogger().info("-------- Dharma drop {} to {}", JSONObject.toJSONString(up), JSONObject.toJSONString(element));
                CatMgr.copyElementType(element, up);
                CatMgr.clearElementTypeInfo(up);
                CatMgr.removeElementAttr(up, eElementAttrTypes.Anger.getType());
                break;
            }
        }
        super.generateNext(context);
    }
}
